home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / demos / texobj.c < prev    next >
C/C++ Source or Header  |  1998-12-15  |  7KB  |  282 lines

  1. /* $Id: texobj.c,v 3.0 1998/02/14 18:42:29 brianp Exp $ */
  2.  
  3. /*
  4.  * Example of using the 1.1 texture object functions.
  5.  * Also, this demo utilizes Mesa's fast texture map path.
  6.  *
  7.  * Brian Paul   June 1996   This file is in the public domain.
  8.  */
  9.  
  10.  
  11. /*
  12.  * $Log: texobj.c,v $
  13.  * Revision 3.0  1998/02/14 18:42:29  brianp
  14.  * initial rev
  15.  *
  16.  */
  17.  
  18.  
  19. #include <math.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "GL/glut.h"
  23.  
  24. static GLuint Window = 0;
  25.  
  26. static GLuint TexObj[2];
  27. static GLfloat Angle = 0.0f;
  28. static GLboolean HaveTexObj = GL_FALSE;
  29.  
  30.  
  31. #if defined(GL_VERSION_1_1)
  32. #  define TEXTURE_OBJECT 1
  33. #elif defined(GL_EXT_texture_object)
  34. #  define TEXTURE_OBJECT 1
  35. #  define glBindTexture(A,B)     glBindTextureEXT(A,B)
  36. #  define glGenTextures(A,B)     glGenTexturesEXT(A,B)
  37. #  define glDeleteTextures(A,B)  glDeleteTexturesEXT(A,B)
  38. #endif
  39.  
  40.  
  41.  
  42.  
  43. static void draw( void )
  44. {
  45.    glDepthFunc(GL_EQUAL);
  46.    /*   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );*/
  47.    glClear( GL_COLOR_BUFFER_BIT );
  48.  
  49.    glColor3f( 1.0, 1.0, 1.0 );
  50.  
  51.    /* draw first polygon */
  52.    glPushMatrix();
  53.    glTranslatef( -1.0, 0.0, 0.0 );
  54.    glRotatef( Angle, 0.0, 0.0, 1.0 );
  55.    if (HaveTexObj) {
  56. #ifdef TEXTURE_OBJECT
  57.       glBindTexture( GL_TEXTURE_2D, TexObj[0] );
  58. #endif
  59.    }
  60.    else {
  61.       glCallList( TexObj[0] );
  62.    }
  63.    glBegin( GL_POLYGON );
  64.    glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
  65.    glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
  66.    glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
  67.    glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
  68.    glEnd();
  69.    glPopMatrix();
  70.  
  71.    /* draw second polygon */
  72.    glPushMatrix();
  73.    glTranslatef( 1.0, 0.0, 0.0 );
  74.    glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
  75.    if (HaveTexObj) {
  76. #ifdef TEXTURE_OBJECT
  77.       glBindTexture( GL_TEXTURE_2D, TexObj[1] );
  78. #endif
  79.    }
  80.    else {
  81.       glCallList( TexObj[0] );
  82.    }
  83.    glBegin( GL_POLYGON );
  84.    glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
  85.    glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
  86.    glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
  87.    glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
  88.    glEnd();
  89.    glPopMatrix();
  90.  
  91.    glutSwapBuffers();
  92. }
  93.  
  94.  
  95.  
  96. static void idle( void )
  97. {
  98.    Angle += 2.0;
  99.    glutPostRedisplay();
  100. }
  101.  
  102.  
  103.  
  104. /* change view Angle, exit upon ESC */
  105. static void key(unsigned char k, int x, int y)
  106. {
  107.    switch (k) {
  108.       case 27:
  109. #ifdef TEXTURE_OBJECT
  110.      glDeleteTextures( 2, TexObj );
  111. #endif
  112.      glutDestroyWindow(Window);
  113.      exit(0);
  114.    }
  115. }
  116.  
  117.  
  118.  
  119. /* new window size or exposure */
  120. static void reshape( int width, int height )
  121. {
  122.    glViewport(0, 0, (GLint)width, (GLint)height);
  123.    glMatrixMode(GL_PROJECTION);
  124.    glLoadIdentity();
  125.    /*   glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
  126.    glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
  127.    glMatrixMode(GL_MODELVIEW);
  128.    glLoadIdentity();
  129.    glTranslatef( 0.0, 0.0, -8.0 );
  130. }
  131.  
  132.  
  133. static void init( void )
  134. {
  135.    static int width=8, height=8;
  136.    static GLubyte tex1[] = {
  137.      0, 0, 0, 0, 0, 0, 0, 0,
  138.      0, 0, 0, 0, 1, 0, 0, 0,
  139.      0, 0, 0, 1, 1, 0, 0, 0,
  140.      0, 0, 0, 0, 1, 0, 0, 0,
  141.      0, 0, 0, 0, 1, 0, 0, 0,
  142.      0, 0, 0, 0, 1, 0, 0, 0,
  143.      0, 0, 0, 1, 1, 1, 0, 0,
  144.      0, 0, 0, 0, 0, 0, 0, 0 };
  145.  
  146.    static GLubyte tex2[] = {
  147.      0, 0, 0, 0, 0, 0, 0, 0,
  148.      0, 0, 0, 2, 2, 0, 0, 0,
  149.      0, 0, 2, 0, 0, 2, 0, 0,
  150.      0, 0, 0, 0, 0, 2, 0, 0,
  151.      0, 0, 0, 0, 2, 0, 0, 0,
  152.      0, 0, 0, 2, 0, 0, 0, 0,
  153.      0, 0, 2, 2, 2, 2, 0, 0,
  154.      0, 0, 0, 0, 0, 0, 0, 0 };
  155.  
  156.    GLubyte tex[64][3];
  157.    GLint i, j;
  158.  
  159.  
  160.    glDisable( GL_DITHER );
  161.  
  162.    /* Setup texturing */
  163.    glEnable( GL_TEXTURE_2D );
  164.    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
  165.    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
  166.  
  167.  
  168.    /* generate texture object IDs */
  169.    if (HaveTexObj) {
  170. #ifdef TEXTURE_OBJECT
  171.       glGenTextures( 2, TexObj );
  172. #endif
  173.    }
  174.    else {
  175.       TexObj[0] = glGenLists(2);
  176.       TexObj[1] = TexObj[0]+1;
  177.    }
  178.  
  179.    /* setup first texture object */
  180.    if (HaveTexObj) {
  181. #ifdef TEXTURE_OBJECT
  182.       glBindTexture( GL_TEXTURE_2D, TexObj[0] );
  183. #endif
  184.    }
  185.    else {
  186.       glNewList( TexObj[0], GL_COMPILE );
  187.    }
  188.    /* red on white */
  189.    for (i=0;i<height;i++) {
  190.       for (j=0;j<width;j++) {
  191.      int p = i*width+j;
  192.      if (tex1[(height-i-1)*width+j]) {
  193.         tex[p][0] = 255;   tex[p][1] = 0;     tex[p][2] = 0;
  194.      }
  195.      else {
  196.         tex[p][0] = 255;   tex[p][1] = 255;   tex[p][2] = 255;
  197.      }
  198.       }
  199.    }
  200.  
  201.    glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
  202.          GL_RGB, GL_UNSIGNED_BYTE, tex );
  203.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  204.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  205.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  206.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  207.    if (!HaveTexObj) {
  208.       glEndList();
  209.    }
  210.    /* end of texture object */
  211.  
  212.    /* setup second texture object */
  213.    if (HaveTexObj) {
  214. #ifdef TEXTURE_OBJECT
  215.       glBindTexture( GL_TEXTURE_2D, TexObj[1] );
  216. #endif
  217.    }
  218.    else {
  219.       glNewList( TexObj[1], GL_COMPILE );
  220.    }
  221.    /* green on blue */
  222.    for (i=0;i<height;i++) {
  223.       for (j=0;j<width;j++) {
  224.      int p = i*width+j;
  225.      if (tex2[(height-i-1)*width+j]) {
  226.         tex[p][0] = 0;     tex[p][1] = 255;   tex[p][2] = 0;
  227.      }
  228.      else {
  229.         tex[p][0] = 0;     tex[p][1] = 0;     tex[p][2] = 255;
  230.      }
  231.       }
  232.    }
  233.    glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
  234.          GL_RGB, GL_UNSIGNED_BYTE, tex );
  235.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  236.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  237.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  238.    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  239.    if (!HaveTexObj) {
  240.       glEndList();
  241.    }
  242.    /* end texture object */
  243.  
  244. }
  245.  
  246.  
  247.  
  248. int main( int argc, char *argv[] )
  249. {
  250.    glutInitWindowPosition(0, 0);
  251.    glutInitWindowSize(300, 300);
  252.    glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
  253.  
  254.    Window = glutCreateWindow("Texture Objects");
  255.    if (!Window) {
  256.       exit(1);
  257.    }
  258.  
  259.    /* check that renderer has the GL_EXT_texture_object extension
  260.     * or supports OpenGL 1.1
  261.     */
  262. #ifdef TEXTURE_OBJECT
  263.    {
  264.       char *exten = (char *) glGetString( GL_EXTENSIONS );
  265.       char *version = (char *) glGetString( GL_VERSION );
  266.       if (   strstr( exten, "GL_EXT_texture_object" )
  267.       || strncmp( version, "1.1", 3 )==0 ) {
  268.      HaveTexObj = GL_TRUE;
  269.       }
  270.    }
  271. #endif
  272.  
  273.    init();
  274.  
  275.    glutReshapeFunc( reshape );
  276.    glutKeyboardFunc( key );
  277.    glutIdleFunc( idle );
  278.    glutDisplayFunc( draw );
  279.    glutMainLoop();
  280.    return 0;
  281. }
  282.